<template>
  <div :style="{height:nowheight , width:nowwidth}">
    <Echart
      v-if="chartstau"
      :options="options"
    />
    <!-- :height="nowheight"
      :width="nowwidth" -->
  </div>
</template>

<script>
import Echart from "../../baseMain/index.vue";
export default {
  components: {
    Echart
  },
  props: {
    zhinterval: {
      type: String || Number,
      default: () => "auto"
    },
    zhrotate: {
      type: String,
      default: () => "0"
    },
    isshowleg: {
      type: Boolean,
      default: () => false
    },
    nowwidth: {
      type: String,
      default: () => "300px"
    },
    nowheight: {
      type: String,
      default: () => "200px"
    },
    cdata: {
      type: Object,
      default: () => ({})
    },
    fontcolor: {
      type: String,
      default: () => "#fff"
    },
    fontSize: {
      type: Number,
      default: () => 18
    }
  },
  data() {
    return {
      chartstau: true,
      options: {}
    };
  },
  watch: {
    cdata: {
      handler(newData) {
        this.options = {
          //   title: {
          //     text: newData.title,
          //     textStyle: {
          //         color:'#fff',
          //         fontSize:'12',
          //     },
          //     left: 'center'
          //   },
          color: [
            "#5B8FF9",
            "#5AD8A6",
            "#F6BD16",
            "#ffdb5c",
            "#ff9f7f",
            "#32c5e9",
            "#fb7293",
            "#e7bcf3"
          ],
          tooltip: {
            trigger: "axis",
            formatter: function (params) {
              var relVal = params[0].name;
              let unitName = ''
              if (newData.unitName !== undefined) {
                unitName = newData.unitName || ''
              }
              for (var i = 0, l = params.length; i < l; i++) {
                relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + params[i].value + '' + unitName || ""
              }
              // console.log(relVal)
              return relVal;
            }
          },
          toolbox: {
            show: true
          },
          grid: {
            left: "0",
            right: "20",
            bottom: "20",
            top: "30",
            containLabel: true
          },
          xAxis: {
            type: "category",
            // boundaryGap: false,
            boundaryGap: true,
            data: newData.xAxisdata,
            axisLabel: {
              showMinLabel: false,
              showMaxLabel: false,
              interval: this.zhinterval,
              rotate: this.zhrotate,
              margin: 10,
              fontSize: this.fontSize,
              color: this.fontcolor
            }
          },
          yAxis: [
            {
              name: newData.unitName || '',
              position: 'left',
              type: "value",
              axisLabel: {
                fontSize: 10,
                color: this.fontcolor
              },
              minInterval: 1,
              min: 0
            },
            {
              show: newData.rightShow,
              position: 'right',
              type: "value",
              axisLabel: {
                fontSize: 10,
                color: this.fontcolor
              },
              minInterval: 1,
              min: 0
            }
          ],
          calculable: true,
          legend: {
            show: this.isshowleg,
            // orient: "horizontal",
            // icon: "circle",
            bottom: 0,
            x: "center",
            data: newData.xData
            // axisLabel: {
            //   color: "#fff"
            // }
            // selected: true
            // textStyle: {
            //   color: "#fff"
            // }
          },
          series: newData.seriesData
        };
      },
      immediate: true,
      deep: true
    },
    // 应为最内层的高度固定为百分之百，所以写这里
    nowheight: {
      handler(e) {
        this.nowheights = e
        this.resetsty()
      },
      deep: true
    }
  },
  methods: {
    // 重置样式
    resetsty() {
      // alert("jkjk")
      this.chartstau = false
      this.$nextTick(
        () => {
          this.chartstau = true
        }
      )
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
